# coding:utf-8
"""
@author: jing
@contact: 529548204@qq.com
@file: serializer.py
@time: 2022/5/24 16:56
"""
from .models import *
from platformuser.serializer import UserSerializers
from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer


class ProjectSerializers(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    describe = serializers.CharField(required=False, allow_null=True, allow_blank=True)
    usersinfo = serializers.SerializerMethodField(read_only=True, required=False)
    modulesnum = serializers.SerializerMethodField(read_only=True, required=False)

    class Meta(object):
        model = Myproject
        fields = [
            'id',
            'describe',
            'projectname',
            'usersinfo',
            'testrunner',
            'modulesNum',
            'create_time',
            'update_time',
        ]

        extra_kwargs = {
            "projectname": {
                "required": True
            },
        }

    @staticmethod
    def get_usersinfo(obj):
        user = obj.user
        d = {'username': user.username, 'userid': user.id}
        return d

    @staticmethod
    def get_modulesnum(obj):
        d = 0
        return d

    def to_representation(self, instance):
        data = super().to_representation(instance)
        return data

    def validate(self, attrs):
        data = super().validate(attrs)
        user = self.context.get("user")
        data["user"] = user
        return data
